package com.example.asus.yuejian_10.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.base.BaseActivity;
import com.example.asus.yuejian_10.bean.ListResult_1;
import com.example.asus.yuejian_10.imple.Home_actimple;
import com.example.asus.yuejian_10.model.Model;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ActJoinActivity extends BaseActivity {
    private EditText uname,tel,child_num,adult_num,people_1,people_2,id_1,id_2;
    private TextView act_price_adult,act_price_child,
                     act_join_total_num,totalPay;
    private Button act_join_count;
    private ListResult_1 info;
    private RadioGroup payway;
    private TextView paynotice;
    private String paytype,pay,insurance;
    //购买数量，总价格
    private int total,tomoney,total_1,total_2;
    //学生的总价格和成人价格,总价格
    private double t_c_m,t_a_m,a_t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_join);
        init();
        setInfo();
        getNumAndPrice();
        getInfo();
        event();
    }
  private void getNumAndPrice() {

      adult_num.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              //先得到学生的票数，并保存起来
              if (!s.toString().equals(""))
              {
                  //得到输入的数字
                  total_2=Integer.parseInt(s.toString());
                  total=total_1+total_2;
                  act_join_total_num.setText(String.valueOf(total));
                  t_a_m=total_2*(Integer.parseInt(info.getActivity_price()));
                  a_t=t_a_m+t_c_m;
                  totalPay.setText(String.valueOf(a_t));
              }

          }

          @Override
          public void afterTextChanged(Editable s) {

              if (!s.toString().equals(""))
              {
                  total_2=Integer.parseInt(s.toString());
                  total=total_1+total_2;
                  act_join_total_num.setText(String.valueOf(total));

                  t_a_m=total_2*(Integer.parseInt(info.getActivity_price()));
                  a_t=t_a_m+t_c_m;
                  totalPay.setText(String.valueOf(a_t));
              }
          }
      });
        child_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //先得到学生的票数，并保存起来
                if (!s.toString().equals(""))
                {
                    //得到输入的数字
                    total_1=Integer.parseInt(s.toString());
                    total=total_1+total_2;
                    act_join_total_num.setText(String.valueOf(total));
                    t_c_m=total_1*(Integer.parseInt(info.getActivity_price_stu()));
                    a_t=t_a_m+t_c_m;
                    totalPay.setText(String.valueOf(a_t));

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!s.toString().equals(""))
                {
                    total_1=Integer.parseInt(s.toString());
                    total=total_1+total_2;
                    act_join_total_num.setText(String.valueOf(total));

                    t_c_m=total_1*(Integer.parseInt(info.getActivity_price_stu()));
                    a_t=t_a_m+t_c_m;
                    totalPay.setText(String.valueOf(a_t));
                }
            }
        });
    }

    private void getInfo() {
        int kk=Integer.parseInt(info.getActivity_price())*Integer.parseInt(adult_num.getText().toString())+Integer.parseInt(info.getActivity_price_stu())
                *Integer.parseInt(child_num.getText().toString());

        pay=String.valueOf(kk);
        totalPay.setText("合计："+pay);
        insurance=people_1.getText().toString()+":"+id_1.getText().toString()+";"
                +people_2.getText().toString()+":"+id_2.getText().toString();
    }

    private void setInfo() {
        int t=Integer.parseInt(adult_num.getText().toString())+Integer.parseInt(child_num.getText().toString());
        act_join_total_num.setText(String.valueOf(t));
        //设置总价格
        tomoney=Integer.parseInt(info.getActivity_price().toString())*Integer.parseInt(adult_num.getText().toString())
                +Integer.parseInt(info.getActivity_price_stu().toString())*Integer.parseInt(child_num.getText().toString());
        totalPay.setText(String.valueOf(tomoney));
        //设置初始价格
        act_price_child.setText("学生票价格￥"+info.getActivity_price_stu());
        act_price_adult.setText("成人票价格￥"+info.getActivity_price());
    }

    private void event() {
        payway.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case R.id.act_join_alipay:
                        paytype="0";
                        break;
                    case R.id.act_join_weixinpay:
                        paytype="1";
                        break;
                    default:
                        Toast.makeText(ActJoinActivity.this,"您还没有选择支付方式",Toast.LENGTH_SHORT).show();
                }
            }
        });
        act_join_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (paytype==null||paytype=="")
                {
                    Toast.makeText(ActJoinActivity.this,"您还没有选择支付方式",Toast.LENGTH_SHORT).show();
                    return;
                }
                final Retrofit re= Model.getConn();
                Home_actimple impls=re.create(Home_actimple.class);
                Call<Integer> call=impls.joinAct(
                        uname.getText().toString(),
                        info.getId(),
                        tel.getText().toString(),
                        adult_num.getText().toString(),
                        child_num.getText().toString(),
                        paytype,pay,paynotice.getText().toString()
                        ,insurance,getUserId(),"pf2cmg5jk9p24f2mv1kjf55bo3"

                );
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if (response.body()!=null){
                            Toast.makeText(ActJoinActivity.this,"加入成功"+response.body(),Toast.LENGTH_LONG).show();
                              /*Intent i=new Intent(ActJoinActivity.this,CountActivity.class);
                              i.putExtra("account_id",response.body());
                              startActivity(i);*/
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {
                        Toast.makeText(ActJoinActivity.this,"加入失败",Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    private void init() {
        total=0;
        tomoney=0;
        paynotice=(TextView)findViewById(R.id.act_notice);
        payway=(RadioGroup)findViewById(R.id.act_payway) ;
        uname=(EditText)findViewById(R.id.act_join_uname);
        tel=(EditText)findViewById(R.id.act_join_tel);
        totalPay=(TextView)findViewById(R.id.pay);
        act_join_total_num=(TextView)findViewById(R.id.act_join_total_num);
        adult_num=(EditText)findViewById(R.id.act_join_adult_num);
        child_num=(EditText)findViewById(R.id.act_join_child_num);
        act_price_adult=(TextView)findViewById(R.id.act_price_adult);
        act_price_child=(TextView)findViewById(R.id.act_price_child);
        people_1=(EditText)findViewById(R.id.act_join_people_1);
        people_2=(EditText)findViewById(R.id.act_join_people_2);
        id_1=(EditText)findViewById(R.id.act_join_id_1);
        id_2=(EditText)findViewById(R.id.act_join_id_2);
        act_join_count=(Button)findViewById(R.id.act_join_count);
        info=(ListResult_1)getIntent().getParcelableExtra("actinfo");
        t_a_m=0.0;
        t_c_m=0.0;
        a_t=0.0;

    }


}
